//
//  ViewController.m
//  qq
//
//  Created by 王成龙 on 16/9/20.
//  Copyright © 2016年 wcl. All rights reserved.
//

#import "ViewController.h"
#import "ChatViewController.h"
#import "LoginData.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITextField *password;

@end

@implementation ViewController
- (IBAction)onCome:(id)sender {
    [self.name becomeFirstResponder];
}
- (IBAction)onRise:(id)sender {
    [self.name resignFirstResponder];
}
- (IBAction)onClick:(id)sender {
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://xl18z.cn/login.php?name=%@&password=%@",self.name.text, self.password.text]];
    NSURLRequest *req = [NSURLRequest requestWithURL:url];
    NSURLSession *session = [NSURLSession sharedSession];
    
    NSURLSessionDataTask *task = [session dataTaskWithRequest:req completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error) {
            NSLog(@"%@",error.description);
            return;
            
        }
        // 获取到数据了。
        NSString *text = [[NSString alloc] initWithBytes:data.bytes length:data.length encoding:NSUTF8StringEncoding];
        NSLog(@"%@",text);
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        LoginData *d = [[LoginData alloc]initWithDictionary:dict];
        // GCD 大中枢派发
        dispatch_async(dispatch_get_main_queue(), ^{
            // 刷新界面代码
            // [dict[@"errcode"
            if (d.errcode == 0  ) {
                ChatViewController *con = [self.storyboard instantiateViewControllerWithIdentifier:@"aaa"];
                [self.navigationController pushViewController:con animated:YES];
            }else{
                NSLog(@"%@",d.errinfo);//d.errinfodict[@"errinfo"]
            }
        });
    }];
    [task resume];
    
}
//警告
- (IBAction)OnAlert:(id)sender {
    UIAlertController *con = [UIAlertController alertControllerWithTitle:@"座右铭" message:@"努力到无能为力，拼搏到感动自己" preferredStyle: UIAlertControllerStyleAlert];
    UIAlertController *act1 = [UIAlertAction actionWithTitle:@"坚持" style:UIAlertActionStyleCancel handler:nil];
    
    UIAlertController *act2 = [UIAlertAction actionWithTitle:@"奋斗" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action){}];
    [con addAction:act1];
    [con addAction:act2];    [self presentViewController:con animated:YES completion:nil];
    
}
/*- (IBAction)OnShare:(id)sender {
UIActivityViewController *con = [UIActivityViewController]
}*/

- (IBAction)onPhotos:(id)sender {
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
